package com.ljc.cjgl.controller;

import java.util.HashSet;
import java.util.List;

import org.abyss.data.specifications.Rule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ljc.cjgl.custom.TablePageable;
import com.ljc.cjgl.custom.TableResult;
import com.ljc.cjgl.entitiy.Marking;
import com.ljc.cjgl.entitiy.TbTest;
import com.ljc.cjgl.entitiy.Tea;
import com.ljc.cjgl.entitiy.Teacher;
import com.ljc.cjgl.form.MarkingForm;
import com.ljc.cjgl.service.MarkingService;
import com.ljc.cjgl.service.TeaService;
import com.ljc.cjgl.service.TeacherService;
import com.ljc.cjgl.service.TestService;

@Controller
@RequestMapping(value="/marking")
public class MarkingController {
	@Autowired
	private MarkingService service;
	@Autowired
	private TestService testService;
	@Autowired
	private TeaService teacherService;
	@RequestMapping(value="/manage")
	public String manage(ModelMap map) {
		map.put("tests", testService.findAll());
		return "marking/manage";
	}
	
	public Specification<Marking> buildSpec(MarkingForm form){
		HashSet<Rule> rules=new HashSet<>();
		Integer testId = form.getTestId();
		if(testId!=null) {
			rules.add(Rule.property("test.id").equal(testId));
		}
		String teacherName = form.getTeacherName();
		if(StringUtils.hasText(teacherName)) {
			rules.add(Rule.property("teacher.name").contain(teacherName));
		}
		String search=form.getSearch();
		if(StringUtils.hasText(search)) {
			HashSet<Rule> rules2=new HashSet<>();
			rules2.add(Rule.property("test.name").contain(search));
			rules2.add(Rule.property("teacher.name").contain(search));
			rules.add(Rule.or(rules2));
		}
		return Rule.and(rules).toSpecification();
	}
	
	@RequestMapping("/page")
	@ResponseBody
	public Object page(TablePageable pageable,MarkingForm form) {
		PageRequest pageRequest = pageable.bulidPageRequest();
		Specification<Marking> spec = buildSpec(form);
		Page<Marking> page = service.findAll(spec, pageRequest);
		return TableResult.bulidPageResult(page);
	}
	
	@RequestMapping("/edit")
	public String edit(Integer id,Integer testId,ModelMap map) {
		Marking model=new Marking();
		if(id!=null) {
			model=service.findById(id);
		}
		map.put("model",model);
		List<Tea> teachers=teacherService.findAll();
		map.put("teachers", teachers);
		List<TbTest> tests=testService.findAll();
		if(testId!=null) {
			tests.clear();
			tests.add(testService.findOne(testId));
		}
		map.put("tests",tests);
		return "marking/edit";
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public Object save(MarkingForm form) {
		service.save(form);
		return TableResult.bulidResult(true, "数据保存成功");
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(@RequestBody List<Marking> markings) {
		boolean success=service.delete(markings);
		if(success) {
			return TableResult.bulidResult(true, "数据删除成功");
		}else {
			return TableResult.bulidResult(false, "数据删除失败");
		}
	}
}
